package com.example.project.service.impl;

import com.example.project.common.MyTool;
import com.example.project.dao.SafetyTrainDao;
import com.example.project.dto.SafetyTrainBinDTO;
import com.example.project.dto.SafetyTrainBinPageDTO;
import com.example.project.entity.SafetyTrain;
import com.example.project.entity.SafetyTrainBin;
import com.example.project.dao.SafetyTrainBinDao;
import com.example.project.service.SafetyTrainBinService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (SafetyTrainBin)表服务实现类
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@Service("safetyTrainBinService")
public class SafetyTrainBinServiceImpl implements SafetyTrainBinService {
    @Resource
    private SafetyTrainBinDao safetyTrainBinDao;
    @Resource
    private SafetyTrainDao safetyTrainDao;

    private MyTool myTool = new MyTool();
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SafetyTrainBin queryById(Integer id) {
        return this.safetyTrainBinDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param safetyTrain 筛选条件
     * @param pageRequest 分页对象
     * @param startTime
     * @param endTime
     * @return 查询结果
     */
    @Override
    public Page<SafetyTrainBinPageDTO> queryByPage(SafetyTrain safetyTrain, PageRequest pageRequest, Date startTime, Date endTime) {
        //删除到期的数据
        this.deleteAllOldTime();

        long total = this.safetyTrainDao.count(safetyTrain);
        List<SafetyTrainBinPageDTO> safetyTrainBinPageDTOS = new ArrayList<>();
        safetyTrain.setEntRecBin(2);
        //培训日期的开始时间与结束时间
        Date day = startTime;
        Date nextDay = endTime;

        safetyTrain.setTrainDate(startTime);

        List<SafetyTrain> safetyTrains = this.safetyTrainDao.queryAllByLimit(safetyTrain, pageRequest,null,null,day,nextDay);

        for (SafetyTrain e:safetyTrains){
            SafetyTrainBinPageDTO dto = new SafetyTrainBinPageDTO();
            dto.setSafetyTrain(e);
            dto.setSafetyTrainBin(this.safetyTrainBinDao.queryBySafetyTrainId(e.getSafetyTrainId()));
            safetyTrainBinPageDTOS.add(dto);
        }

        return new PageImpl<>(safetyTrainBinPageDTOS, pageRequest, total);
    }
/**
 * 删除所有到期的时间
 * */
    private void deleteAllOldTime() {
        List<SafetyTrainBin> list = this.safetyTrainBinDao.queryAll();
        for (SafetyTrainBin e:list){
            Date date = myTool.dateSomeNOw(e.getBinTime(),e.getDeleteTime());
            Date date1 = myTool.dateNow();
            if (date1.before(date)) {
                //"date1 在 date 之后"

            } else if (date1.after(date)) {
                //"date1 在 date 之前"
                this.deleteById1(e.getId());
            } else {
                this.deleteById1(e.getId());
               //"date1 和 date 相等"
            }
        }
    }

    /**
     * 新增数据
     *
     * @param safetyTrainBin 实例对象
     * @return 实例对象
     */
    @Override
    public SafetyTrainBin insert(SafetyTrainBin safetyTrainBin) {


        this.safetyTrainBinDao.insert(safetyTrainBin);
        return safetyTrainBin;
    }

    /**
     * 修改数据
     *
     * @param safetyTrainBin 实例对象
     * @return 实例对象
     */
    @Override
    public SafetyTrainBin update(SafetyTrainBin safetyTrainBin) {
        this.safetyTrainBinDao.update(safetyTrainBin);
        return this.queryById(safetyTrainBin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.safetyTrainBinDao.deleteById(id) > 0;
    }

    /**
     * 实现彻底删除
     */
    @Override
    public boolean deleteById1(Integer data) {
        //删除管理表中的数据
        SafetyTrainBin safetyTrainBin = this.safetyTrainBinDao.queryById(data);
        this.safetyTrainDao.deleteById(safetyTrainBin.getSafetyTrainId());
        return this.deleteById(data);
    }
    /**
     * 恢复功能
     */
    @Override
    public boolean update1(SafetyTrainBin safetyTrainBin) {
        SafetyTrain safetyTrain  = this.safetyTrainDao.queryById(safetyTrainBin.getSafetyTrainId());
        safetyTrain.setEntRecBin(1);
        this.safetyTrainDao.update(safetyTrain);
        return this.deleteById(safetyTrainBin.getId());
    }

    @Override
    public Boolean deleteById2(SafetyTrainBinDTO data) {
        boolean flog1 = false;
        boolean flog2 = false;
        if (data.getBinIds() != null){
            List<Integer> listSTB = myTool.myToList(data.getBinIds());
            flog1 = this.safetyTrainBinDao.deleteByIdAll(listSTB)>0;
        }

        if (data.getIds() != null){
            List<Integer> listST = myTool.myToList(data.getIds());
            flog2 = this.safetyTrainDao.deleteByIdAll(listST)>0;
        }

        return flog2&&flog1;
    }

    @Override
    public Boolean update2(SafetyTrainBinDTO data) {
        boolean flog1 = false;
        boolean flog2 = false;
        if (data.getBinIds() != null){
            List<Integer> listSTB = myTool.myToList(data.getBinIds());
            flog1 = this.safetyTrainBinDao.deleteByIdAll(listSTB)>0;
        }

        if (data.getIds() != null){
            List<Integer> listST = myTool.myToList(data.getIds());
            flog2 = this.safetyTrainDao.batchUpdate(listST)>0;
        }
        return flog2&&flog1;
    }
}
